package ru.stepuk.modern.nodes.servers;

import javax.swing.ImageIcon;
import ru.stepuk.modern.Model;
import ru.stepuk.modern.Request;
import ru.stepuk.modern.Timeline;
import ru.stepuk.modern.distributions.Distribution;
import ru.stepuk.modern.events.ReleaseServer;

/**
 *
 * @author ivan
 */
public class SimpleServer extends Server {

    {
        icon = new ImageIcon(getClass().getResource("/images/d_norm.png"));
    }

    public SimpleServer(Model model, Timeline timeline, Distribution distribution) {
        super(model, timeline, distribution);
    }

    @Override
    public void processRequest(Request request) {
        System.out.println(timeline.now() + ": " + name + "request come to server");
        if (!idle) {
            if ((buffer != null) && (buffer.hasFreeSpace())) {
                buffer.putRequest(request);
                System.out.println(timeline.now() + ": " + name + "request goes to buffer");
            } else {
                System.out.println(timeline.now() + ": " + name + "request failed");
            }
        } else {
            // Normal
            
            idle = false;
            currentRequest = request;
            setReleaseEvent();
        }

    }

    private void setReleaseEvent() {
        double x = timeline.now() + distribution.nextValue();
        timeline.addEvent(x, new ReleaseServer(this));
    }
}
